<template>
  <!-- 弹框组件 -->
  <el-form label-width="120px" :model="form" ref="ruleForm" :rules="rules">
    <el-form-item label="所属学科" label-width="80px" v-if="!$route.query.id" prop="subjectID">
      <el-select v-model="form.subjectID" placeholder="请选择" style="width: 100%">
        <el-option
          v-for="item in simpleTag"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="目录名称" label-width="80px" prop="tagName">
      <el-input v-model="form.tagName" style="width: 100%" placeholder="请输入目录名称" />
    </el-form-item>
    <!-- 确认取消按钮 -->
    <el-form-item>
      <el-button @click="cancel">取消</el-button>
      <el-button type="primary" @click="submitForm">确认</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { add, detail, update } from '@/api/hmmm/tags.js'
import { simple } from '@/api/hmmm/subjects.js'

export default {
  name: 'AddTags',
  props: {
    isEdit: {
      type: Boolean,
      required: true
    },
    id: {
      type: [String, Number],
      required: true
    },
    subjectDetail: {
      type: Object,
      require: true
    }
  },
  data () {
    return {
      form: {
        subjectID: null, // 学科ID
        tagName: '', // 目录名称
        subjectName: '' // 所属学科
      },
      simpleTag: [], // 简单列表
      rules: {
        subjectID: [
          { required: true, message: '请输入所属学科', trigger: 'change' }
        ],
        tagName: [
          { required: true, message: '请输入目录名称', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.detail()
    this.list()
  },
  methods: {
    // 回填数据
    async detail () {
      if (this.isEdit) {
        if (!this.subjectDetail.subjectName) {
          this.subjectDetail.subjectID = ''
          this.form.tagName = this.subjectDetail.tagName
        } else {
          //   this.form.subjectID = res.data.subjectID
          const res = await detail({ id: this.id })
          this.form.subjectID = res.data.subjectID
          //   this.form.subjectName = this.subjectDetail.subjectName
          this.form.tagName = this.subjectDetail.tagName
        }
      }
    },

    // 取消
    cancel () {
      this.$emit('closeDialog')
      this.$refs.ruleForm.resetFields()
      this.form = {
        subjectID: null, // 学科ID
        tagName: '', // 目录名称
        subjectName: '' // 所属学科
      }
    },

    //  提交
    async submitForm () {
      this.$refs.ruleForm.validate(async valid => {
        if (!valid) return false
        // 添加目录
        if (!this.isEdit) {
          if (this.$route.query.id) {
            this.form.subjectID = Number(this.$route.query.id)
          }
          const params = { subjectID: this.form.subjectID, tagName: this.form.tagName }
          await add(params)
        } else {
        // 修改目录
        //   const params = { subjectID: this.form.subjectID, tagName: this.form.tagName }
          await update({ id: this.id, subjectID: this.form.subjectID, tagName: this.form.tagName })
        }
        // 刷新列表
        this.$emit('closeDialog')
        // 成功提示
        this.$message.success(this.isEdit ? '修改成功' : '添加成功')
      })
    },

    // 下拉框获取数据
    async list () {
      const res = await simple()
      this.simpleTag = res.data
    }

  }
}
</script>

<style lang="scss" scoped>
label.el-form-item__label {
  width: 80px;
}
</style>
